package cc.comac.mom.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cc.comac.mom.bean.BusLogSearch;
import cc.comac.mom.dao.BusLogDao;
import cc.comac.mom.entity.BusLogEntity;


@Service
public class BusLogServiceImpl implements BusLogService{
	@Autowired
	private BusLogDao busLogDao;

	@Override
	public int save(BusLogEntity busLogEntity) {
		int existTable = busLogDao.existTable("mom_log");
		if(existTable==0) {
			int createTable = busLogDao.createTable("mom_log","mom_log_id");
			if(createTable<1) {
				return 0;
			}
		}
		
		int save = busLogDao.save(busLogEntity, "mom_log","mom_log_id");
		return save;
	}

	@Override
	public int save(BusLogEntity busLogEntity, String tableName) {
		if(!tableName.endsWith("_log")) {
			tableName +="_log";
		}
		String idName = tableName+"_id";
		int existTable = busLogDao.existTable(tableName);
		if(existTable==0) {
			int createTable = busLogDao.createTable(tableName, idName);
			if(createTable<1) {
				return 0;
			}
		}
		int save = busLogDao.save(busLogEntity, tableName,idName);
		return save;
	}

	@Override
	public int save(List<BusLogEntity> busLogs) {
		int existTable = busLogDao.existTable("mom_log");
		if(existTable==0) {
			int createTable = busLogDao.createTable("mom_log","mom_log_id");
			if(createTable<1) {
				return 0;
			}
		}
		int save = busLogDao.saveBatch(busLogs, "mom_log","mom_log_id");
		return save;
	}

	@Override
	public int save(List<BusLogEntity> busLogs, String tableName) {
		if(!tableName.endsWith("_log")) {
			tableName +="_log";
		}
		String idName = tableName+"_id";
		int existTable = busLogDao.existTable(tableName);
		if(existTable==0) {
			int createTable = busLogDao.createTable(tableName, idName);
			if(createTable<1) {
				return 0;
			}
		}
		int save = busLogDao.saveBatch(busLogs,tableName,idName);
		return save;
	}

	@Override
	public int delete(long id) {
		int existTable = busLogDao.existTable("mom_log");
		if(existTable==0) {
			return 0;
		}
		int delete = busLogDao.delete(id, "mom_log","mom_log_id");
		return delete;
	}

	@Override
	public int delete(long id, String tableName) {
		if(!tableName.endsWith("_log")) {
			tableName +="_log";
		}
		String idName = tableName+"_id";
		int existTable = busLogDao.existTable(tableName);
		if(existTable==0) {
			return 0;
		}
		int delete = busLogDao.delete(id, tableName,idName);
		return delete;
	}

	@Override
	public int delete(BusLogSearch busLogSearch) {
		int existTable = busLogDao.existTable("mom_log");
		if(existTable==0) {
			return 0;
		}
		int deleteLog = busLogDao.deleteLog(busLogSearch, "mom_log","mom_log_id");
		return deleteLog;
	}

	@Override
	public int delete(BusLogSearch busLogSearch, String tableName) {
		if(!tableName.endsWith("_log")) {
			tableName +="_log";
		}
		String idName = tableName+"_id";
		int existTable = busLogDao.existTable(tableName);
		if(existTable==0) {
			return 0;
		}
		int deleteLog = busLogDao.deleteLog(busLogSearch, tableName, idName);
		return deleteLog;
	}

	@Override
	public BusLogEntity getLog(long id) {
		int existTable = busLogDao.existTable("mom_log");
		if(existTable==0) {
			return null;
		}
		return busLogDao.getById(id, "mom_log","mom_log_id");
	}

	@Override
	public BusLogEntity getLog(long id, String tableName) {
		if(!tableName.endsWith("_log")) {
			tableName +="_log";
		}
		String idName = tableName+"_id";
		int existTable = busLogDao.existTable(tableName);
		if(existTable==0) {
			return null;
		}
		return busLogDao.getById(id, tableName,idName);
	}

	@Override
	public List<BusLogEntity> getLogs(BusLogSearch busLogSearch) {
		int existTable = busLogDao.existTable("mom_log");
		if(existTable==0) {
			return null;
		}
		return busLogDao.getLogs(busLogSearch, "mom_log","mom_log_id");
	}

	@Override
	public List<BusLogEntity> getLogs(BusLogSearch busLogSearch, String tableName) {
		if(!tableName.endsWith("_log")) {
			tableName +="_log";
		}
		String idName = tableName+"_id";
		int existTable = busLogDao.existTable(tableName);
		if(existTable==0) {
			return null;
		}
		
		return busLogDao.getLogs(busLogSearch, tableName,idName);
	}
}
